/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.CFields;
import DTO.Message;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MessageBUS extends IData<Message>{

    @Override
    public Message add(Message item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Message item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Message> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Message Find(Message item, List<Message> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    /*
	Minh
	Add body: t=0 Insert; t=1 Update 
    */
    @Override
    protected Object OnSubmit(Message item, int t, SubConfig config) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        Object result = 0;
        String mysql ="";
        PreparedStatement cs;      
        try 
        {
            if(t == 0)
            {
                mysql = "insert into " + CFields.tb_message + "(" + CFields.user_id + "," 
                        + CFields.userid_receive  + ","
                        + CFields.description  + "," + CFields.state  + ","
                        + CFields.create_date  + "," + CFields.is_read  + ","
                        + CFields.project_id  + "," + CFields.is_group  + "," + CFields.isdelete
                        +  ") " 
                        + " values(?,?,?,?,?,?,?,?,?)";                
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);  
                //cs.setLong(1, item.getId());
                cs.setLong(1, item.getUser_id());
                cs.setLong(2, item.getUserid_receive());
                cs.setString(3, item.getDescription());
                cs.setBoolean(4, item.getState());
                cs.setTimestamp(5,item.getCreate_date());
                cs.setBoolean(6, false);
                cs.setLong(7,item.getProject_id());
                cs.setBoolean(8, false);
                cs.setBoolean(9, false);
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
            else if(t == 1)
            {
                mysql = "update " + CFields.tb_message + " set "
                        + CFields.user_id + "=? "
                        + CFields.userid_receive + "=? "
                        + CFields.description + "=? "
                        + CFields.state + "=? "
                        + CFields.create_date + "=? "
                        + CFields.is_read + "=? "
                        + CFields.project_id + "=? "
                        + CFields.is_group + "=? "
                        + CFields.isdelete + "=? "
                        + "where " 
                        + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getUser_id());
                cs.setLong(2, item.getUserid_receive());
                cs.setString(3, item.getDescription());
                cs.setBoolean(4, item.getState());
                cs.setTimestamp(5,item.getCreate_date());
                cs.setBoolean(6, false);
                cs.setLong(7,item.getProject_id());
                cs.setBoolean(8, false);
                cs.setBoolean(9, false);
                cs.setLong(10, item.getId());
                result = cs.executeUpdate();
            }
            else
            {
                mysql = "delete from " + CFields.tb_message 
                        + " where "
                        + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getId());
                result = cs.executeUpdate();
            }
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return result;
    }

    @Override
    public Object insertOnSubmit(Message item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Message item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }

    @Override
    public Object deleteOnSubmint(Message item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    /*
        Minh
        Add body
    */
    @Override
    protected Message getItem(ResultSet rs) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        Message item = new Message();
        try {
            item.setUser_id(rs.getLong(CFields.user_id));
            item.setUserid_receive(rs.getLong(CFields.userid_receive));
            item.setDescription(rs.getString(CFields.description));
            item.setState(rs.getBoolean(CFields.state));
            item.setCreate_date(rs.getTimestamp(CFields.create_date));
            item.setIs_read(rs.getBoolean(CFields.is_read));
            item.setProject_id(rs.getLong(CFields.project_id));
            item.setIs_group(rs.getBoolean(CFields.is_group));
            item.setIsdelete(rs.getBoolean(CFields.isdelete));
            item.setId(rs.getLong(CFields.id));
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<Message> getAll(SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
